package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 *
 * 全文检索 controller 组件
 * @author : zjj
 * @date : 2021/2/2 11:58
 */

@RestController
@RequestMapping("api/list")
public class ListApiController {


    /**
     *
     * 引入操作es的api对象
     */
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    /**生成对应的mapping
     *localhost:8203/api/list/inner/createIndex 访问，访问完成，那么mapping 则会自动建立
     * @return 处理结果
     */
    @GetMapping("inner/createIndex")
    public Result createIndex(){
        // 创建goods 的index
        restTemplate.createIndex(Goods.class);
        // 创建mapping
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    @Autowired
    private SearchService searchService;

    /** 商品的上架
     *
     * @param skuId skuId
     * @return      处理结果
     */
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /** 商品的下架
     *
     * @param skuId skuId
     * @return      处理结果
     */
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        searchService.lowerGoods(skuId);
        return Result.ok();
    }

    /**
     * 增加商品热度
     * @param skuId skuId
     * @return      处理结果
     */
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable("skuId")Long skuId){
        searchService.incrHotScore(skuId);

        return Result.ok();
    }

    /**
     * 检索数据
     * @param searchParam   查询条件封装
     * @return              返回处理结果
     * @throws IOException  异常
     */
    @PostMapping
    public Result getList(@RequestBody SearchParam searchParam) throws IOException {
        SearchResponseVo search = searchService.search(searchParam);
        return Result.ok(search);
    }
}
